package cn.edu.cdut.service;

import cn.edu.cdut.dao.PlanCourseInfoDao;
import cn.edu.cdut.dao.ResultInfoDao;
import cn.edu.cdut.dao.SpecialityInfoDao;
import cn.edu.cdut.dao.WishInfoDao;
import cn.edu.cdut.dto.Major;
import cn.edu.cdut.dto.Majors;
import cn.edu.cdut.dto.MyRes;
import cn.edu.cdut.model.PlanCourseInfo;
import cn.edu.cdut.model.SpecialityInfo;
import cn.edu.cdut.model.WishInfo;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Iterator;
import java.util.List;

/**
 * Author  kobe
 * Created on 2017-05-26-22:18
 */
@Service
public class SpecialitySetService {

        @Autowired
        private SpecialityInfoDao specialityInfoDao;

        @Autowired
        private PlanCourseInfoDao planCourseInfoDao;

        @Autowired
        private WishInfoDao wishInfoDao;

        @Autowired
        private ResultInfoDao resultInfoDao;

        //获得所有专业信息
        @Transactional
        public MyRes getAllSpeciality( ) {
                List<SpecialityInfo> specialityInfoList = specialityInfoDao.findAll();
                //转换为Json字符串
                ObjectMapper mapper = new ObjectMapper();
                Iterator iter = specialityInfoList.iterator();
                String[] resArr = new String[specialityInfoList.size()];
                int i = 0;
                while(iter.hasNext()){
                        try {
                                resArr[i] = mapper.writeValueAsString(iter.next());
                                i++;
                        } catch (JsonProcessingException e) {
                                e.printStackTrace();
                                return new MyRes(201, "获取专业信息失败！", null);
                        }
                }
                return new MyRes(200, null, resArr);
        }

        //保存专业设置
        @Transactional
        public MyRes setSpeciality(Majors majors) {
                String[] specArr = majors.getSpecialities();
                for (String aSpecArr : specArr) {
                        SpecialityInfo specialityInfo = new SpecialityInfo();
                        specialityInfo.setSpeciality(aSpecArr);
                        specialityInfo.setDiscipline(majors.getDiscipline());
                        specialityInfo.setCollege(majors.getCollege());
                        specialityInfo.setIsFinished(false);
                        specialityInfoDao.save(specialityInfo);
                }
                Iterable<SpecialityInfo> specIterable = specialityInfoDao.findAll();
                //转换为Json字符串
                ObjectMapper mapper1 = new ObjectMapper();
                Iterator specIter = specIterable.iterator();
                String[] resArr = new String[(int)specialityInfoDao.count()];
                int i = 0;
                while(specIter.hasNext()){
                        try {
                                resArr[i] = mapper1.writeValueAsString(specIter.next());
                                i++;
                        } catch (JsonProcessingException e) {
                                e.printStackTrace();
                                return new MyRes(202, "提交失败！", null);
                        }
                }
                return new MyRes(200, "提交成功！", resArr);
        }

        //修改专业信息
        @Transactional
        public MyRes updateSpeciality(Major major) {
                SpecialityInfo specInfo = specialityInfoDao.findOne(major.getId());
                specInfo.setSpeciality(major.getSpeciality());
                specInfo.setDiscipline(major.getDiscipline());
                specInfo.setCollege(major.getCollege());
                ObjectMapper mapper2 = new ObjectMapper();
                String resStr2;
                try {
                        resStr2 = mapper2.writeValueAsString(specialityInfoDao.save(specInfo));
                } catch (JsonProcessingException e) {
                        e.printStackTrace();
                        return new MyRes(201, "修改没有成功！", null);
                }
                return new MyRes(200, "修改成功！", resStr2);
        }

        //单个删除专业信息
        @Transactional
        public MyRes removeSpeciality(Major major) {
                specialityInfoDao.delete(major.getId());
                ObjectMapper mapper3 = new ObjectMapper();
                String resStr3;
                try {
                        resStr3 = mapper3.writeValueAsString(major);
                } catch (JsonProcessingException e) {
                        e.printStackTrace();
                        return new MyRes(201, "删除失败！", null);
                }
                return new MyRes(200, "删除成功！", resStr3);
        }

        //设置人数
        @Transactional
        public MyRes setTotalPeople(Major major) {
                SpecialityInfo specInfo = specialityInfoDao.findOne(major.getId());
                specInfo.setTotalPeople(major.getTotalPeople());
                specInfo.setRemainPeople(major.getTotalPeople());
                ObjectMapper mapper4 = new ObjectMapper();
                String resStr4;
                try {
                        resStr4 = mapper4.writeValueAsString(specialityInfoDao.save(specInfo));
                } catch (JsonProcessingException e) {
                        e.printStackTrace();
                        return new MyRes(201, "设置失败！", null);
                }
                return new MyRes(200, "设置成功！", resStr4);
        }

        //重置录取状态
        @Transactional
        public MyRes resetState() {
                //重置专业状态
                List<SpecialityInfo> specialityInfoList = specialityInfoDao.findAll();
                for (SpecialityInfo specialityInfo : specialityInfoList) {
                        specialityInfo.setRemainPeople(specialityInfo.getTotalPeople());
                        specialityInfo.setIsFinished(false);
                        specialityInfoDao.save(specialityInfo);
                }
                //重置志愿状态
                String gradeID = null;
                Iterable<PlanCourseInfo> planCourseInfos = planCourseInfoDao.findAll();
                Iterator<PlanCourseInfo> iterator = planCourseInfos.iterator();
                if (iterator.hasNext()) {
                        PlanCourseInfo planCourseInfo = iterator.next();
                        gradeID = planCourseInfo.getGrade();
                }
                List<WishInfo> wishInfoList = wishInfoDao.findByGrade(gradeID);
                for (WishInfo wishInfo : wishInfoList) {
                        wishInfo.setEnrolled(false);
                        wishInfoDao.save(wishInfo);
                }
                //删除数据库结果数据
                resultInfoDao.deleteAll();
                return new MyRes(200, "重置成功！", null);
        }

        //获得某大类的所有专业
        @Transactional
        public MyRes getSpecOption(String subject) {
                List<SpecialityInfo> specList = specialityInfoDao.findByDiscipline(subject);
                ObjectMapper mapper5 = new ObjectMapper();
                Iterator iter = specList.iterator();
                String[] resArr = new String[specList.size()];
                int i = 0;
                while(iter.hasNext()){
                        try {
                                resArr[i] = mapper5.writeValueAsString(iter.next());
                                i++;
                        } catch (JsonProcessingException e) {
                                e.printStackTrace();
                                return new MyRes(201, "获取志愿选项失败！", null);
                        }
                }
                return new MyRes(200, null, resArr);
        }
}
